package com.sjtu.rental.common;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.sjtu.rental.entity.SysVariable;
import com.sjtu.rental.services.VariableService;

public class StartupListener extends ContextLoaderListener implements
		ServletContextListener {

	@Override
	public void contextInitialized(ServletContextEvent event) {
		VariableService variableService = (VariableService) WebApplicationContextUtils
				.getRequiredWebApplicationContext(event.getServletContext())
				.getBean("variableService");
		List<SysVariable> districts = variableService
				.getVarsList(VariableService.DISTRICT);
		event.getServletContext().setAttribute("districts", districts);
		Map<Integer, String> districtsMap = new HashMap<Integer, String>();
		for (SysVariable d : districts) {
			districtsMap.put(d.getCode(), d.getName());
		}
		event.getServletContext().setAttribute("districtsMap", districtsMap);
	}
}
